/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.bff.slimserver.musicjukebox.ui.components.radio;

import com.bff.bjj.ui.component.ColorProperties;
import com.bff.slimserver.utils.TableSorter;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JTable;
import javax.swing.JViewport;
import javax.swing.table.TableModel;

/**
 *
 * @author Bill
 */
public class RadioTable extends JTable {
private TableSorter tableSorter;

    public RadioTable() {
        super(); 
   
        setDefaultRenderer(Object.class, new RadioCellRenderer());
        setGridColor(ColorProperties.TABLE_HEADER_BACKGROUND);
        setIntercellSpacing(new Dimension(1, 1));
        setFillsViewportHeight(true);
        setShowGrid(false);
        setShowVerticalLines(true);

    //TableColumnManager.install(this);
    }

    @Override
    public boolean getScrollableTracksViewportHeight() {
        Component parent = getParent();
        if (parent instanceof JViewport) {
            return parent.getHeight() > getPreferredSize().height;
        }
        return false;
    }

    /**
     * Paints empty rows too, after letting the UI delegate do
     * its painting.
     */
    public void paint(Graphics g) {
        super.paint(g);
        paintEmptyRows(g);
    }

    /**
     * Paints the backgrounds of the implied empty rows when the
     * table model is insufficient to fill all the visible area
     * available to us. We don't involve cell renderers, because
     * we have no data.
     */
    protected void paintEmptyRows(Graphics g) {
        final int rowCount = getRowCount();
        final Rectangle clip = g.getClipBounds();
        if (rowCount * rowHeight < clip.height) {
            for (int i = rowCount; i <= clip.height / rowHeight; ++i) {
                g.setColor(colorForRow(i));
                g.fillRect(clip.x, i * rowHeight, clip.width, rowHeight);
            }
        }
    }

    /**
     * Returns the appropriate background color for the given row.
     */
    protected Color colorForRow(int row) {
        return (row % 2 == 0) ? ColorProperties.LIST_BACKGROUND_EVEN : ColorProperties.LIST_BACKGROUND_ODD;
    }

    @Override
    public void setModel(TableModel tableModel) {

        if (tableModel instanceof TableSorter) {
            setTableSorter((TableSorter) tableModel);
        } else {
            setTableSorter(new TableSorter(tableModel));

            if (tableModel instanceof RadioTableModel) {
                setModel((RadioTableModel) tableModel);
            }
        }
        super.setModel(getTableSorter());
    }

    public TableSorter getTableSorter() {
        return tableSorter;
    }

    public void setTableSorter(TableSorter tableSorter) {
        this.tableSorter = tableSorter;
    }
}
